#include <iostream>
using std::cout;
using std::endl;

void test0(){
    int * p = (int*)malloc(sizeof(int));
    *p = 100;
    free(p);
    p = nullptr;

    //<>中是转换的目标类型
    //()中是待转换的内容
    int * p2 = static_cast<int*>(malloc(sizeof(int)));
    *p2 = 100;
    free(p2);
    p2 = nullptr;
}

void test1(){
    const char * pstr = "hello";
    //这是不安全的转换，存在隐患
    /* char * p = (char*)pstr; */
    /* p[0] = 'H'; */

    //static_cast比C风格的转换更安全
    /* char * p2 = static_cast<char*>(pstr); */
}


void test2(){
    const int num = 10;
    /* int a = const_cast<int>(num); */

    //const_cast可以作用于const引用和指向常量的指针
    const int * p = &num;
    /* *p = 100; */
    int * p2 = const_cast<int*>(p);
    *p2 = 100;
    cout << *p2 << endl;
    cout << num << endl;
    cout << p2 << endl;
    cout << &num << endl;
}

int main(void){
    test2();
    return 0;
}
